﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ChungNhanSinhVien.Models;
using ChungNhanSinhVien.Infrastructures;
using ChungNhanSinhVien.Entities;

namespace ChungNhanSinhVien.Controllers
{
    public class Ad_ChungNhanController : Controller
    {
        ChungNhanRepository cnRepo = new ChungNhanRepository();
        LoaiChungNhanRepository loaicnRepo = new LoaiChungNhanRepository();
        //
        // GET: /ChungNhan/        
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult XemChungNhanTheoLoaiCN(int id = -1)
        {
            if (SessionManager.nsr != null)
            {
                //if (id.CompareTo(0) > 0)
                //    SessionManager.loaicnID = id;
                if (SessionManager.loaicnID.CompareTo(0) > 0)
                {
                    ViewData["loaicnID"] = SessionManager.loaicnID;                    
                }
                else
                {
                    ViewData["loaicnID"] = id;                    
                }
                ViewData["tb"] = TempData["tb"] as string ?? string.Empty;
                ViewData["slcn"] = TempData["slcn"] as string ?? string.Empty;
                return View();
            }
            else
            {
                return RedirectToAction("Index", "DangNhap");
            }
        }

        [ChildActionOnly]
        public ActionResult UC_TatCaLoaiChungNhan()
        {            
            List<LoaiChungNhan> list = loaicnRepo.LayTatCaLoaiChungNhan();
            return PartialView(list);
        }

        //        [ChildActionOnly]
        public ActionResult UC_ChungNhanTheoLoai(int id)
        {
            //SessionManager.loaicnID = id;
            List<ChungNhan> list = cnRepo.LayChungNhansTheoLoaiCNID(id);
            return PartialView(list);
        }

        public ActionResult TaoMoiLoaiCN()
        {            
            if (SessionManager.nsr != null)
            {
                ViewData["tb"] = TempData["tb"] as string ?? string.Empty;
                return View();
            }
            else
            {
                return RedirectToAction("Index", "DangNhap");
            }
        }

        [HttpPost]
        public ActionResult TaoMoiLoaiCN(LoaiChungNhan model)
        {
            if (SessionManager.nsr != null)
            {
                TempData["tb"] = "";
                var btn = Request.Params.AllKeys.FirstOrDefault(key => key.Equals("TaoMoi"));
                if (btn != null)
                {
                    if (model.TenLoaiChungNhan == null)
                    {
                        TempData["tb"] = "loirongtenloai";
                        return RedirectToAction("TaoMoiLoaiCN");
                    }

                    if (loaicnRepo.KiemTraTonTaiTheoTen(model.TenLoaiChungNhan, model.ID))
                    {
                        TempData["tb"] = "taomoithatbai_datontai";
                        return RedirectToAction("TaoMoiLoaiCN");
                    }

                    if (loaicnRepo.TaoMoi(model))
                    {
                        TempData["tb"] = "taomoiloaicnthanhcong";
                        return RedirectToAction("XemChungNhanTheoLoaiCN");
                    }
                    else
                    {
                        TempData["tb"] = "taomoithatbai";
                        return RedirectToAction("TaoMoiLoaiCN");
                    }
                }

                var btnTroVe = Request.Params.AllKeys.FirstOrDefault(key => key.Equals("TroVe"));
                if (btnTroVe != null)
                {
                    return RedirectToAction("XemChungNhanTheoLoaiCN");
                }
                return View();
            }
            else
            {
                return RedirectToAction("Index", "DangNhap");
            }
        }

        public ActionResult SuaLoaiCN(int id)
        {
            if (SessionManager.nsr != null)
            {
                var loaiCN = loaicnRepo.LayDoiTuongTheoID(id);
                ViewData["tb"] = TempData["tb"] as string ?? string.Empty;
                return View(loaiCN);
            }
            else
            {
                return RedirectToAction("Index", "DangNhap");
            }
        }

        [HttpPost]
        public ActionResult SuaLoaiCN(LoaiChungNhan model)
        {
            if (SessionManager.nsr != null)
            {
                TempData["tb"] = "";
                var btn = Request.Params.AllKeys.FirstOrDefault(key => key.Equals("Sua"));
                if (btn != null)
                {
                    if (model.TenLoaiChungNhan == null)
                    {
                        TempData["tb"] = "loirongtenloai";
                        return RedirectToAction("SuaLoaiCN");
                    }

                    if (loaicnRepo.KiemTraTonTaiTheoTen(model.TenLoaiChungNhan, model.ID))
                    {
                        TempData["tb"] = "capnhatloaicnthatbai_datontai";
                        return RedirectToAction("SuaLoaiCN");
                    }

                    if (loaicnRepo.CapNhat(model))
                    {
                        TempData["tb"] = "capnhatloaicnthanhcong";
                        return RedirectToAction("XemChungNhanTheoLoaiCN");
                    }
                    else
                    {
                        TempData["tb"] = "capnhatthatbai";
                        return RedirectToAction("SuaLoaiCN");
                    }
                }

                var btnTroVe = Request.Params.AllKeys.FirstOrDefault(key => key.Equals("TroVe"));
                if (btnTroVe != null)
                {
                    return RedirectToAction("XemChungNhanTheoLoaiCN");
                }

                return View(model);
            }
            else
            {
                return RedirectToAction("Index", "DangNhap");
            }
        }

        public ActionResult XoaLoaiCN(int id)
        {            
            if (SessionManager.nsr != null)
            {
                TempData["tb"] = "";
                TempData["slcn"] = "";
                int slcn_theolcn;
                if (loaicnRepo.Xoa(id, out slcn_theolcn))
                {
                    TempData["tb"] = "xoaloaicnthanhcong";
                }
                else
                {
                    TempData["slcn"] = slcn_theolcn.ToString();
                    TempData["tb"] = "xoaloaicnthatbai";
                }
                return RedirectToAction("XemChungNhanTheoLoaiCN");
            }
            else
            {
                return RedirectToAction("Index", "DangNhap");
            }
        }


        public ActionResult TaoMoiCN()
        {
            if (SessionManager.nsr != null)
            {
                ChungNhan model = new ChungNhan();
                model.loaiCNList = loaicnRepo.LayTatCaLoaiChungNhanSelectList();
                ViewData["tb"] = TempData["tb"] as string ?? string.Empty;
                return View(model);
            }
            else
            {
                return RedirectToAction("Index", "DangNhap");
            }
        }

        [HttpPost]
        public ActionResult TaoMoiCN(ChungNhan model)
        {
            if (SessionManager.nsr != null)
            {
                TempData["tb"] = "";
                var btn = Request.Params.AllKeys.FirstOrDefault(key => key.Equals("TaoMoi"));
                if (btn != null)
                {
                    if (model.TenChungNhan == null)
                    {
                        TempData["tb"] = "loirongtencn";
                        return RedirectToAction("TaoMoiCN");
                    }

                    model.LoaiChungNhanID = int.Parse(model.maloai);
                    if (cnRepo.KiemTraTonTaiTheoTenVaLoaiCN(model.TenChungNhan, model.LoaiChungNhanID.Value, model.ID))
                    {
                        TempData["tb"] = "taomoithatbai_datontai";
                        return RedirectToAction("TaoMoiCN");
                    }
                    if (cnRepo.TaoMoi(model))
                    {
                        TempData["tb"] = "taomoicnthanhcong";
                        return RedirectToAction("XemChungNhanTheoLoaiCN");
                    }
                    else
                    {
                        TempData["tb"] = "taomoithatbai";
                        return RedirectToAction("TaoMoiCN");
                    }
                }

                var btnTroVe = Request.Params.AllKeys.FirstOrDefault(key => key.Equals("TroVe"));
                if (btnTroVe != null)
                {
                    return RedirectToAction("XemChungNhanTheoLoaiCN");
                }
                return View(model);
            }
            else
            {
                return RedirectToAction("Index", "DangNhap");
            }
        }

        public ActionResult SuaCN(int id)
        {
            if (SessionManager.nsr != null)
            {
                var model = cnRepo.LayDoiTuongTheoID(id);
                model.loaiCNList = loaicnRepo.LayTatCaLoaiChungNhanSelectList();
                model.maloai = model.LoaiChungNhanID.ToString();
                ViewData["tb"] = TempData["tb"] as string ?? string.Empty;
                return View(model);
            }
            else
            {
                return RedirectToAction("Index", "DangNhap");
            }
        }

        [HttpPost]
        public ActionResult SuaCN(ChungNhan model)
        {
            TempData["tb"] = "";
            if (SessionManager.nsr != null)
            {
                var btn = Request.Params.AllKeys.FirstOrDefault(key => key.Equals("Sua"));
                if (btn != null)
                {
                    if (model.TenChungNhan == null)
                    {
                        TempData["tb"] = "loirongtencn";
                        return RedirectToAction("SuaCN");
                    }
                                        
                    model.LoaiChungNhanID = int.Parse(model.maloai);
                    if (cnRepo.KiemTraTonTaiTheoTenVaLoaiCN(model.TenChungNhan, model.LoaiChungNhanID.Value, model.ID))
                    {
                        TempData["tb"] = "capnhatcnthatbai_datontai";
                        return RedirectToAction("SuaCN");
                    }
                    
                    if (cnRepo.CapNhat(model))
                    {
                        TempData["tb"] = "capnhatcnthanhcong";
                        return RedirectToAction("XemChungNhanTheoLoaiCN");
                    }
                    else
                    {
                        TempData["tb"] = "capnhatthatbai";                 
                        return RedirectToAction("SuaCN");
                    }
                }

                var btnTroVe = Request.Params.AllKeys.FirstOrDefault(key => key.Equals("TroVe"));
                if (btnTroVe != null)
                {
                    return RedirectToAction("XemChungNhanTheoLoaiCN");
                }
                return View();
            }
            else
            {
                return RedirectToAction("Index", "DangNhap");
            }
        }

        public ActionResult XoaCN(int id)
        {
            TempData["tb"] = "";
            if (SessionManager.nsr != null)
            {
                if (cnRepo.Xoa(id))
                {
                    TempData["tb"] = "xoacnthanhcong";
                }
                else
                {
                    TempData["tb"] = "xoacnthatbai";
                }
                return RedirectToAction("XemChungNhanTheoLoaiCN");
            }
            else
            {
                return RedirectToAction("Index", "DangNhap");
            }
        }
    }
}
